#
# Copyright 2008 Charles Connell <charles@connells.org>
#
# This file is part of Tuxedo.
#
# Tuxedo is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Tuxedo is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Tuxedo; if not, see <http://gnu.org/licenses/.>
#

import os
import sys
import gtk
import gtk.gdk

# Constants for the indexes in a tuple describing status options
STATUS_BOLD = 0
STATUS_ITALIC = 1
STATUS_UNDERLINE = 2
STATUS_STRIKETHROUGH = 3

class StatusConfig:
    """Creates the dialog box for configuring Tuxedo."""

    def get_options(self):
        """Returns options user has toggled."""
        return self.options

    def get_name(self):
        """Returns name of status user has entered."""
        return self.nameentry.get_text()

    def onBoldToggle(self, data):
        """Remember bold setting and show it."""
        self.options[STATUS_BOLD] = data.get_active()
        self.resetLabel()

    def onItalicToggle(self, data):
        """Remember italic setting and show it."""
        self.options[STATUS_ITALIC] = data.get_active()
        self.resetLabel()

    def onUnderlineToggle(self, data):
        """Remember underline setting and show it."""
        self.options[STATUS_UNDERLINE] = data.get_active()
        self.resetLabel()

    def onStrikethroughToggle(self, data):
        """Remember strikethrough setting and show it."""
        self.options[STATUS_STRIKETHROUGH] = data.get_active()
        self.resetLabel()

    def resetLabel(self):
        """Set example label to use status options."""
        label = self.widgets.get_widget('statuslabel')

        # These two string hold the Pango Markup Language tags that describe how the text should look
        openingtags = ''
        closingtags = ''

        # Add tags for options that are enabled
        if self.options[STATUS_BOLD] is True:
            openingtags += '<b>'
        if self.options[STATUS_ITALIC] is True:
            openingtags += '<i>'
        if self.options[STATUS_UNDERLINE] is True:
            openingtags += '<u>'
        if self.options[STATUS_STRIKETHROUGH] is True:
            openingtags += '<s>'

        # End those same tags
        if self.options[STATUS_STRIKETHROUGH] is True:
            closingtags += '</s>'
        if self.options[STATUS_UNDERLINE] is True:
            closingtags += '</u>'
        if self.options[STATUS_ITALIC] is True:
            closingtags += '</i>'
        if self.options[STATUS_BOLD] is True:
            closingtags += '</b>'

        # Set label to have text with pango markup
        label.set_markup (openingtags + 'The status will look like this' + closingtags)

        # Redraw label at next event loop go-round
        label.queue_draw()

    def __init__(self, options, name):
        """Reads the glade file and creates the dialog."""
        # Set the path to the glade file to be the path to StatusConfig.glade
        gladefile = os.path.join(os.path.dirname(sys.argv[0]), 'StatusConfig.glade')

        # Read the widgets from the glade file and connect the signals
        self.widgets = gtk.glade.XML(gladefile)
        self.dialog = self.widgets.get_widget('StatusConfig')
        self.nameentry = self.widgets.get_widget('nameentry')
        signals = {"on_BoldToggle" : self.onBoldToggle,
                   "on_ItalicToggle" : self.onItalicToggle,
                   "on_UnderlineToggle" : self.onUnderlineToggle,
                   "on_StrikethroughToggle" : self.onStrikethroughToggle,
                   }
        self.widgets.signal_autoconnect(signals)

        # Remember widgets from glade UI
        self.boldtoggle = self.widgets.get_widget('boldtoggle')
        self.italictoggle = self.widgets.get_widget('italictoggle')
        self.underlinetoggle = self.widgets.get_widget('underlinetoggle')
        self.strikethroughtoggle = self.widgets.get_widget('strikethroughtoggle')

        # Make local copy of options
        self.options = list(options)

        # Set toggle buttons 'toggled' for appropriate settings
        self.boldtoggle.set_active (self.options[STATUS_BOLD])
        self.italictoggle.set_active (self.options[STATUS_ITALIC])
        self.underlinetoggle.set_active (self.options[STATUS_UNDERLINE])
        self.strikethroughtoggle.set_active (self.options[STATUS_STRIKETHROUGH])

        # Make label reflect settings
        self.resetLabel()

        # Initialize name entry to current name of status
        self.nameentry.set_text(name)
